//    Copyright (C) Kherty.  All rights reserved.
#region

using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Security;

#endregion

namespace OpenLS.UI.Internals
{
    public class StatePriorityConverter : TypeConverter
    {
        // Methods
        public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType)
        {
            switch (Type.GetTypeCode(sourceType))
            {
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.String:
                    return true;
            }
            return false;
        }

        public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
        {
            if (destinationType != typeof (InstanceDescriptor))
            {
                return (destinationType == typeof (string));
            }
            return true;
        }

        public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo,
                                           object source)
        {
            bool auto;
            if (source == null)
            {
                throw GetConvertFromException(source);
            }
            if (source is string)
            {
                return FromString((string) source, cultureInfo);
            }
            var num = Convert.ToDouble(source, cultureInfo);
            if (double.IsNaN(num))
            {
                num = 1.0;
                auto = true;
            }
            else
            {
                auto = false;
            }
            return new StatePriority(num, auto);
        }

        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo,
                                         object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if ((value != null) && (value is StatePriority))
            {
                var priority = (StatePriority) value;
                if (destinationType == typeof (string))
                {
                    return ToString(priority, cultureInfo);
                }
                if (destinationType == typeof (InstanceDescriptor))
                {
                    return
                        new InstanceDescriptor(
                            typeof (StatePriority).GetConstructor(new[] {typeof (double), typeof (bool)}),
                            new object[] {priority.Value, priority.IsEnabled});
                }
            }
            throw GetConvertToException(value, destinationType);
        }

        internal static StatePriority FromString(string s, CultureInfo cultureInfo)
        {
            switch (s.ToLower())
            {
                case "never":
                case "disabled":
                    return StatePriority.Disabled;
                default:
                    return new StatePriority(double.Parse(s));
            }
        }

        internal static string ToString(StatePriority priority, CultureInfo cultureInfo)
        {
            return priority.IsEnabled == false ? "Never" : priority.Value.ToString();
        }
    }
}